package com.skychyn;

import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.skychyn.database.SolarDBAdapter;
import com.skychyn.draw.AverageTemperatureChart;
import com.skychyn.draw.DataHandler;
import com.skychyn.draw.IDemoChart;
import com.skychyn.xml.XMLWeatherParser;

public class MainActivity extends Activity {

	// ===========================================================
	// Fields
	// ===========================================================

	// Web connection preferences
	String url = "http://informer.gismeteo.ru/xml/34519_1.xml";
	String ip;
	int port;
	boolean useProxy;

	DataHandler dh;
	SolarDBAdapter dbAdapter;
	XMLWeatherParser dataFiller;
	Context context;

	int dbRecordsCount;

	Cursor solarDatas;

	String outString;

	InputStream is;

	// TextViews
	TextView outputTV;

	// Buttons
	Button refreshButton;
	Button setupButton;
	Button graphicsButton;


	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = getApplicationContext();

		setContentView(R.layout.main);

		refreshButton = (Button) findViewById(R.id.Refresh);
		setupButton = (Button) findViewById(R.id.Setup);
		graphicsButton = (Button) findViewById(R.id.Graph);

		outputTV = (TextView) findViewById(R.id.WebContent);

		// Connect to DB
		dbAdapter = new SolarDBAdapter(this);
		dbAdapter.open();
		
		// Create data handler
		dh = new DataHandler(dbAdapter);

		refreshButton.setOnClickListener(onRefreshButton);
		setupButton.setOnClickListener(onSetupButton);
		graphicsButton.setOnClickListener(onGraphicsButton);
		
		sendDataToList();
	}

	OnClickListener onRefreshButton = new View.OnClickListener() {
		public void onClick(View v) {
			refreshData();
		}
	};

	OnClickListener onSetupButton = new View.OnClickListener() {
		public void onClick(View v) {			
			Intent intent = new Intent(MainActivity.this, SetupActivity.class);
			startActivity(intent);
		}
	};
	
	OnClickListener onGraphicsButton = new View.OnClickListener() {
		public void onClick(View v) {			
			IDemoChart tempChart = new AverageTemperatureChart(dh);
			Intent intent = tempChart.execute(MainActivity.this); //new Intent(MainActivity.this, DrawActivity.class);
			startActivity(intent);
		}
	};

	private void refreshData() {

		// Restore preferences
		SharedPreferences settings = getSharedPreferences("SolarInfo", 0);

		// Parse XML data
		dataFiller = new XMLWeatherParser(dbAdapter, context, settings);
		dataFiller.execute((Void)null);
		sendDataToList();
	}

	private void sendDataToList() {

		outString = "";

		solarDatas = dbAdapter.fetchAllSolarData();
		startManagingCursor(solarDatas);
		dbRecordsCount = solarDatas.getCount();

		// Get all data from solarDatas
		if (solarDatas.moveToFirst()) {
			do {
				String date = solarDatas.getString(solarDatas
						.getColumnIndexOrThrow(SolarDBAdapter.KEY_DATE));

				String press = solarDatas.getString(solarDatas
						.getColumnIndexOrThrow(SolarDBAdapter.KEY_PRES));

				String temp = solarDatas.getString(solarDatas
						.getColumnIndexOrThrow(SolarDBAdapter.KEY_TEMP));

				outString += date + " > P: " + press + " T: " + temp + "\n";

			} while (solarDatas.moveToNext());
		}
		solarDatas.close();
		outputTV.setText(outString);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (dbAdapter != null) {
			dbAdapter.close();
		}
	}

}